<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ConfigController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        return view('admin.config.index');
    }

    public function getConfig()
    {
        $list = DB::table('sys_config')->pluck('value', 'key');
        return successResp($list);
    }

    public function saveConfig(Request $request)
    {
        //验证表单
        $this->validate($request, [
            'key' => ['required','string', 'max:30'],
            'value' => ['string'],
        ]);
        DB::table('sys_config')->where('key', $request->key)->update(['value'=>$request->value]);
        return successResp('');
    }

    /**
     * 获取老兵活动奖励
     * @return \Illuminate\Http\JsonResponse
     */
    public function getLbConfig()
    {
        $defaultValue = [
            8 => [0,0,0,0,0,0,0]
        ];
        $value = DB::table('sys_config')->where('key', 'lb_activity_conf')->value('value');
        if ($value) {
            $defaultValue = json_decode($value);
        }
        return successResp($defaultValue);
    }

    /**
     * 设置老兵活动
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function setLbConfig(Request $request)
    {
        //验证表单
        $this->validate($request, [
            'value' => [],
        ]);
        DB::table('sys_config')->where('key','lb_activity_conf')->update(['value'=>json_encode($request->value)]);
        return successResp('');
    }
}
